package org.example.controller;


import org.example.model.Experiment;
import org.example.model.ExperimentStats;
import org.example.model.Student;
import org.example.model.StudentStats;
import org.example.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api/stats")
public class StatController {
    @Autowired
    private StatService statService;

    @GetMapping("/students")
    public ResponseEntity<Map<Student, StudentStats>> getStudentStats() {
        return new ResponseEntity<>(statService.getStudentStats(), HttpStatus.OK);
    }

    @GetMapping("/experiments")
    public ResponseEntity<Map<Experiment, ExperimentStats>> getExperimentStats() {
        return new ResponseEntity<>(statService.getExperimentStats(), HttpStatus.OK);
    }

    @GetMapping("/export/excel/students")
    public ResponseEntity<byte[]> exportStudentStatsToExcel() throws IOException {
        Map<Student, StudentStats> studentStatsMap = statService.getStudentStats();
        byte[] excelData = ExcelExporter.exportStudentStats(studentStatsMap);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        headers.add("Content-Disposition", "attachment; filename=student-stats.xlsx");

        return new ResponseEntity<>(excelData, headers, HttpStatus.OK);
    }

    @GetMapping("/export/excel/experiments")
    public ResponseEntity<byte[]> exportExperimentStatsToExcel() throws IOException {
        Map<Experiment, ExperimentStats> experimentStatsMap = statService.getExperimentStats();
        byte[] excelData = ExcelExporter.exportExperimentStats(experimentStatsMap);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        headers.add("Content-Disposition", "attachment; filename=experiment-stats.xlsx");

        return new ResponseEntity<>(excelData, headers, HttpStatus.OK);
    }

    @GetMapping("/export/csv/students")
    public ResponseEntity<String> exportStudentStatsToCsv() {
        // 实现 CSV 导出逻辑
        return new ResponseEntity<>("学生实验统计 CSV 内容", HttpStatus.OK);
    }

    @GetMapping("/export/csv/experiments")
    public ResponseEntity<String> exportExperimentStatsToCsv() {
        // 实现 CSV 导出逻辑
        return new ResponseEntity<>("实验提交统计 CSV 内容", HttpStatus.OK);
    }

    @GetMapping("/chart/submission-rate")
    public ResponseEntity<byte[]> getSubmissionRateChart() throws IOException {
        Map<Experiment, ExperimentStats> experimentStatsMap = statService.getExperimentStats();
        byte[] chartData = ChartGenerator.generateSubmissionRateChart(experimentStatsMap);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "image/png");

        return new ResponseEntity<>(chartData, headers, HttpStatus.OK);
    }
}